import {IExtent} from "./OfflineMaps/CollectionClass";

export class GisUtil {
    /**
     * 将极值点按照坐标偏移进行均等分割
     * @param {number[][]} rangs 极值坐标
     * @param {number} xOffset x偏移
     * @param {number} yOffset y偏移
     * @returns {any[]}
     */
    public static splitAveRangByOffset(rangs: number[][], xOffset: number = 0.050, yOffset: number = 0.045) {
        let xMax = 0;
        let yMax = -90;
        let xMin = 180;
        let yMin = 90;
        for (let xy of rangs) {
            xMax = xMax > xy[0] ? xMax : xy[0];
            xMin = xMin < xy[0] ? xMin : xy[0];
            yMax = yMax > xy[1] ? yMax : xy[1];
            yMin = yMin < xy[1] ? yMin : xy[1];
        }
        const xs = [];
        const ys = [];
        for (let x = xMin; x < xMax + xOffset; x += xOffset) {
            xs.push(x);
        }
        for (let y = yMax; y > yMin - xOffset; y -= yOffset) {
            ys.push(y);
        }
        const result = [];
        for (let i = 0; i < xs.length - 1; i++) {
            for (let j = 0; j < ys.length - 1; j++) {
                const x1 = xs[i].toFixed(6);
                const y1 = ys[j].toFixed(6);
                const x2 = xs[i + 1].toFixed(6);
                const y2 = ys[j + 1].toFixed(6);
                result.push(`${x1},${y1}|${x2},${y2}`)
            }
        }
        return result
    }


    public static getExtent(polygons:number[][][][]):IExtent {
        const [onex,oney] = polygons[0][0][0];
        let extent: IExtent={xmax:onex,xmin:onex,ymax:oney,ymin:oney};
        for(let polygon of polygons){
            for(let rings of polygon){
                for(let ring of rings){
                    const [x,y] = ring;
                    if(x > extent.xmax){
                        extent.xmax = x;
                    }
                    if(x < extent.xmin){
                        extent.xmin = x;
                    }
                    if(y > extent.ymax){
                        extent.ymax = y;
                    }
                    if(y < extent.ymin){
                        extent.ymin = y;
                    }
                }
            }
        }
        return extent;
    }
}
